package com.ke.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ke.dao.VideoMapper;
import com.ke.pojo.*;
import com.ke.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author bochen
 */
@Service
public class VideoServiceImpl implements VideoService {
    public RePackage rePackage=new RePackage();
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public RePackage add(Video video) {
//设置上传时间，注意时间格式匹配
        video.setCreate_time(new Date());
        videoMapper.add(video);
        rePackage.setCode(0);
        rePackage.setMessage("上传成功");
        return rePackage;
    }

    @Override
    public RePackage findid(Integer id) {
        Video findidx = videoMapper.findid(id);
        //放的地址值
        rePackage.setVideocname(findidx.getVname());
        rePackage.setVideourl(findidx.getVaddress());
        rePackage.setCode(0);
        return rePackage;
    }

    @Override
    public RePackage getall(Integer currue11, Integer size11) {
        //分页插件，currue11起始页码，size11每页展示条数
        //底层，是通过size11，来判断页面数，判断起始条数
        PageHelper.startPage(currue11,size11);
        List<Video> list = videoMapper.findall();
        //总条数
        Integer findcount = videoMapper.findcount();
        PageInfo<Video> pageInfo = new PageInfo<Video>(list);
        rePackage.setCode(0);
        rePackage.setData(list);
        rePackage.setTotal(findcount);
        return rePackage;
    }

    @Override
    public RePackage findpriceall(Integer size) {
        //看redis中是否有值
        Object listvideo1 = redisTemplate.opsForList().index("listvideo", 0);
        if(listvideo1==null){
            List<Video> finavideoall = videoMapper.finavideoall();
            redisTemplate.opsForList().leftPushAll("listvideo",finavideoall);
        }
       //获取指定长度的redis中的值
        List<Video> listvideo=redisTemplate.opsForList().range("listvideo",0,size-1);
        //也可以不通过redis直接使用此方法在数据库中获取数据
        //List<Video> findpriceall = videoMapper.findpriceall(size);
        rePackage.setCode(0);
        rePackage.setData(listvideo);
        return rePackage;
    }




    @Override
    public RePackage likeadd(Integer id) {
        videoMapper.addlike(id);
        rePackage.setTotal(videoMapper.findlike(id));
        System.out.println(videoMapper.findlike(id));
        rePackage.setCode(0);
        rePackage.setMessage("success");
        return rePackage;
    }

    @Override
    public RePackage findlike(Integer id) {
        Integer findlike = videoMapper.findlike(id);

        rePackage.setTotal(findlike);
        rePackage.setMessage("success");
        rePackage.setCode(0);
        return rePackage;
    }

    @Override
    //开启事物
    @Transactional
    public RePackage addcollect(Integer vid, Integer uid) {
        //只增加收藏数
        VideoCollect findaddlike = videoMapper.findaddlike(uid, vid);
        if(findaddlike!=null){
            rePackage.setMessage("你已收藏");
            rePackage.setCode(505);
            return rePackage;
        }
        videoMapper.addcollect(vid);
        //增加用户的收藏数
        videoMapper.adduidcollect(vid,uid,new Date());
        rePackage.setTotal(videoMapper.findcollect(vid));
        rePackage.setCode(0);
        rePackage.setMessage("success");
        return rePackage;
    }

    @Override
    public RePackage findcollect(Integer id) {
        Integer findcollect = videoMapper.findcollect(id);
        rePackage.setMessage("success");
        rePackage.setCode(0);
        rePackage.setTotal(findcollect);
        return rePackage;
    }

    @Override
    public RePackage addcomment(Integer vid, Integer uid, String comment_words) {
        videoMapper.addcomment(vid,uid,new Date(),comment_words);
        List<VideoCommentqian> list=new ArrayList<VideoCommentqian>();
        for(VideoComment videoComment:videoMapper.findcomment(vid)){
            VideoCommentqian video=new VideoCommentqian();
            String findusername = videoMapper.findusername(videoComment.getUid());
            video.setName(findusername);
            video.setComment_time(videoComment.getComment_time());
            video.setComment_words(videoComment.getComment_words());
            list.add(video);
        }
        rePackage.setData(list);
        rePackage.setCode(0);
        rePackage.setMessage("success");
        return rePackage;
    }

    @Override
    public RePackage findcomment(Integer id) {
        List<VideoComment> findcomment = videoMapper.findcomment(id);
        rePackage.setCode(0);
        rePackage.setMessage("success");
        List<VideoCommentqian> list=new ArrayList<VideoCommentqian>();
        for(VideoComment videoComment:findcomment){
            VideoCommentqian video=new VideoCommentqian();
            String findusername = videoMapper.findusername(videoComment.getUid());
            video.setName(findusername);
            video.setComment_time(videoComment.getComment_time());
            video.setComment_words(videoComment.getComment_words());
            list.add(video);
        }
        rePackage.setData(list);
        return rePackage;
    }
}
